<?php
/*
 * Created on
 * Date: 2007-12-4
 * Author: William Yang
 */
require_once(dirname(__FILE__)."/../common/constants.php");
require_once(DB_SESSION_DIR_PATH."/db_operator.php");
require_once(DB_SESSION_DIR_PATH."/db_obj_defs.php");
require_once(TRANSACTIONS_DIR_PATH."/trans_common.php");

class CalendarManager extends Calendar {
	public function register() {
		$id = parent::register(CONFIGURATION_DIR_PATH."/create_calendar.xml",
								FALSE);
		return $id;
	}
		
	public function query_by_uids($uid) {
		/*
		 * Query information by the UID specified by $uid.
		 */
		$rows = parent::query_by_uids(CONFIGURATION_DIR_PATH."/calendar_table.xml", $uid);
		
		if ($rows === NULL) {
			return NULL;
		} else {
			$ret = array();
			$b_delegate = new Calendar();
			foreach ($rows as $row) {
				$tmp = $b_delegate->array2copy($row);
				$ret[] = $tmp;
			}
			return $ret;
		}
	}
	
	public static function queryByTime($dtFrom, $dtTo) {
		$db_opera = new DatabaseOperator();
		$db_opera->config(CONFIGURATION_DIR_PATH."/calendar_table.xml");
		$cList = new SqlCondList();
		$cList->add_condition(new SqlCondition("ts_start", SqlCondition::NOLESSTHAN, $dtFrom));
		$cList->add_relation(SqlCondition::_AND);
		$cList->add_condition(new SqlCondition("ts_end", SqlCondition::NOLARGERTHAN, $dtTo));
		$rows = $db_opera->select($cList);
		
		$ret = array();		
		$calendar_delegate = new Calendar();
		if ($rows != null && count($rows) > 0) {
			foreach ($rows as $row) {
				$tmp = $calendar_delegate->array2copy($row);
				$ret[] = $tmp;
			}
		}
		
		return $ret;
	}
	
	public static function deleteByUid($uids) {
		$operator = new DatabaseOperator();
		$operator->config(CONFIGURATION_DIR_PATH."/calendar_table.xml");
		
		$condList = new SqlCondList();
		if (gettype($uids) === gettype(array())) {
			$cnt = count($uids);
			for ($i = 0; $i < $cnt - 1; ++$i) {
				$condList->add_condition(new SqlCondition('uid', SqlCondition::EQUAL, $uids[$i]));
				$condList->add_relation(SqlCondition::_OR);
			}
			$condList->add_condition(new SqlCondition('uid', SqlCondition::EQUAL, $uids[$cnt - 1]));
		} else
			$condList->add_condition(new SqlCondition('uid', SqlCondition::EQUAL, $uids));
		
		$db_stat = $operator->delete($condList);
		return ($db_stat == DbError::DB_OK);
	}
	
}
?>
